package com.kingschan.jeasytag.sql.query;

import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 分页载体
 * @author kingschan
 *date:2013-4-15
 */
public class Pagination {
	
	private int total;//总记录条数
	private int start;//开始索引
	private int end;//结束索引
	private String sql;//生成的sql
	private List<Map<String, Object>> data;//数据
	
	
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	public List<Map<String, Object>> getData() {
		return data;
	}
	public void setData(List<Map<String, Object>> data) {
		this.data = data;
	}
	/**
	 * 格式化成多行记录的josn字符串
	 * @return
	 */
	public JSONArray FormatRowsJsonData(){
		JSONArray jsons = new JSONArray();	
		JSONObject json =null;
		for (int i = 0; i < data.size(); i++) {
			json = new JSONObject(data.get(i));
			jsons.put(json);
		}
		return jsons;
	}
	/**
	 * 格式化成分页的记录
	 * @return
	 * @throws Exception
	 */
	public String FormatPageJsonData() throws Exception{		
		JSONObject json = new JSONObject();
		json.put("total", this.total);
		json.put("rows", FormatRowsJsonData());
		return json.toString();
	}
}